import { getCurrentInstance } from 'vue' 
import { ElMessage } from 'element-plus';
export class RequestUrl { 
    url: string;//获取列表请求路径 
    delUrl: string;//删除路径 
    constructor(url: string, delUrl?: string) {
        this.url = url;
        this.delUrl = delUrl || "";
    }
} 

class PageListTable  {
    requestUrl: RequestUrl;//请求路径
    // url: string;//请求路径
   // 默认属性
    isPage: boolean;//是否分页
    pageIndex: number; //第几页
    pageSize: number; //每页数量
    asc: boolean; //排序
    sortPropName: string;//排序字段
    total= 0; //总数 
    dataForm= {}; // 查询条件
    dataList= []; // 数据列表
    indexMethod = 1; //序号
    dataListLoading= false;// 数据列表，loading状态
    
    viewVisible = false; // 查看弹窗状态 
    addOrUpdateVisible = false;// 新增／更新，弹窗visible状态

    constructor(requestUrl: RequestUrl, dataForm: object,
        isPage?: boolean, 
        pageIndex?: number, pageSize?: number,
        asc?: boolean, sortPropName?: string) {  
        this.requestUrl= requestUrl;
        this.dataForm = dataForm; 
        this.isPage = isPage||false;
        this.pageIndex = pageIndex||1;
        this.pageSize = pageSize||10;
        this.asc = asc||false;
        this.sortPropName = sortPropName||"";
    }

    //获取当前实例
    app = getCurrentInstance()?.appContext.config.globalProperties;

    //获取数据
    async getDataList(){  
       return await this.app?.$Api({
            url: this.requestUrl.url,
            params: { 
                    asc: this.asc,
                    sortPropName: this.sortPropName,
                    pageIndex: this.isPage? this.pageIndex : null,
                    pageSize: this.isPage? this.pageSize : null,
                ...this.dataForm
            }
      }).then((res: any) => { 
                this.dataListLoading = false
                if (res.code !== 0) {
                    this.dataList = []
                    this.total = 0
                    ElMessage.info("获取数据失败");
                    return false; 
               } 
               
                this.total = this.isPage ? res.data.total : 0
                // const data = res.data as [];
                //  const data=  (this.isPage ? res.data.dataCollection : res.data) as [];
                // for (let index = 0; index < data.length; index++) {
                //     dataList?.push(data[index]);
                // }
              return res.data;
          }); 
    }

    pageSizeChangeHandle(val: number){
        this.pageIndex = 1
        this.pageSize = val
        this.getDataList() 
      }
    pageCurrentChangeHandle(val: number){
        this.pageIndex = val
        this.getDataList() 
    }

    //删除
    handleDelete(row: any) { 
        ElMessage.info(JSON.stringify(row));
        console.log(row)
        // const data = { id: row.id };
      
        // this.app?.$Api({
        //     url:this.requestUrl.delUrl,
        //     method:"delete",
        //     params: {
        //         ...data
        //     } 
        // }).then((res: any) => {
        //     if (res.code !== 0) {
        //         ElMessage.info("删除失败");
        //         return false
        //     } else {
        //         ElMessage.info("删除成功");
        //     }
        // })
    }

    //添加数据
    handlerAdd(data: any) { 
        this.addOrUpdateVisible = true;
       console.log(data)
    }

}

export default PageListTable;
